package com.wfs.myapp.controller.bk;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.dto.req.bk.AddGroupReqDto;
import com.wfs.myapp.entity.dto.req.bk.ApproveReqDto;
import com.wfs.myapp.entity.dto.req.bk.GenerateTalkRecordReqDto;
import com.wfs.myapp.entity.dto.req.bk.JoinGroupReqDto;
import com.wfs.myapp.entity.vo.GroupListVo;
import com.wfs.myapp.service.BkGroupService;
import com.wfs.myapp.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bk/group")
@Api(value = "小组", tags = "小组")
@Slf4j
public class GroupController {

    @Autowired
    private BkGroupService bkGroupService;

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "新建/修改组")
    public ApiResult<?> addOrUpdate(@Valid @RequestBody AddGroupReqDto addGroupReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.addOrUpdate(userId, addGroupReqDto);
    }

    @PostMapping("/join")
    @ApiOperation(value = "申请加入")
    public ApiResult<?> join(@Valid @RequestBody JoinGroupReqDto joinGroupReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.join(userId, joinGroupReqDto);
    }

    @PostMapping("/groupProcess")
    @ApiOperation(value = "同意/拒绝")
    public ApiResult<?> groupProcess(@Valid @RequestBody ApproveReqDto approveReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.groupProcess(userId, approveReqDto);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除组员/退出")
    public ApiResult<?> delete(@Valid @RequestBody JoinGroupReqDto joinGroupReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.delete(userId, joinGroupReqDto);
    }

    @GetMapping("/myGroupList")
    @ApiOperation(value = "我的组列表")
    public ApiResult<?> myGroupList(@RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size,
                                    HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.myGroupList(userId, page, size);
    }

    @GetMapping("/groupList")
    @ApiOperation(value = "所有组列表")
    public ApiResult<?> groupList(@RequestParam("page") Integer page,
                                  @RequestParam("size") Integer size,
                                  @RequestParam(value = "groupId", required = false) Long groupId,
                                  HttpServletRequest request
    ) {
        Long myUserId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.groupList(myUserId, groupId, page, size);
    }

    @GetMapping("/groupInfo")
    @ApiOperation(value = "组详情")
    public ApiResult<?> info(@RequestParam("groupId") String groupId, HttpServletRequest request) {
        return bkGroupService.info(groupId);
    }

    @GetMapping("/getSameGroup")
    @ApiOperation(value = "获取两人同组信息")
    public ApiResult<List<GroupListVo>> getSameGroup(@RequestParam(value = "userId") Long userId, HttpServletRequest request) {
        Long myUserId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkGroupService.getSameGroup(userId, myUserId);
    }


}
